﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QWeiBo.ViewModel;
using TencentWeiboSDK.Services;
using TencentWeiboSDK.Model;
using System.Collections.ObjectModel;
using System.Threading;
using TencentWeiboSDK.Services.Util;
using QWeiBoN.Objects;
using System.Collections.Generic;

namespace QWeiBo.ViewModule
{
    public class MainListModel : BaseViewModel
    {
        /// <summary>
        /// 实例化时间线相关 API 服务.
        /// </summary>
        private StatusesService statusService = new StatusesService();

        /// <summary>
        /// 实例化帐户相关 API 服务.
        /// </summary>
        private UserService userService = new UserService();
        private User user;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainListModel()
        {
            Timeline = new List<Status>();
        }

        /// <summary>
        /// 收藏/或者取消收藏一个微博
        /// </summary>
        /// <param name="id"></param>
        /// <param name="action"></param>
        public void FavoriteWeibo(string id, bool isFavorite, Action<Callback<Result>> action)
        {
            // 异步刷新，可以增加用户体验
            new Thread(() =>
            {
                statusService.FavoriteWeiboFromId(new ServiceArgument() { Id = id }, isFavorite, (callback) =>
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        if (null != action)
                        {
                            action(callback);
                        }
                    });
                });
            });
        }

        /// <summary>
        /// 刷新用户时间线需要的数据
        /// </summary>
        public void RefreshUserTimeLine(PageFlag flag, long pagetime, string name, Action<Callback<StatusCollection>> action)
        {
            // 异步刷新，可以增加用户体验
            new Thread(() =>
            {
                statusService.UserTimeLine(new ServiceArgument() { PageFlag = (int)flag, Reqnum = 10, PageTime = pagetime, Name = name }, (callBack) =>
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        if (null != action)
                        {
                            action(callBack);
                        }
                    });
                });
            });
        }

        /// <summary>
        /// 刷新时间线需要的数据
        /// </summary>
        /// <param name="action"></param>
        public void Refresh(PageFlag flag, long pagetime, Action<Callback<StatusCollection>> action)
        {
            // 异步刷新可以让用户有更好的操作体验
            new Thread(() =>
            {
                //// 获得自己的详细资料.
                //userService.UserInfo((userCallBack) =>
                //{
                //    // Callback 结果是当前 AccessToken 的用户数据
                //    // 可以参考 UserInfo 的定义: UserInfo(Action<Callback<User>> callback)
                //    this.user = userCallBack.Data;
                // 刷新Timeline, API的参数是通过ServiceArugment传递，只需要填入你所需要的参数
                // 同样，callback的数据类型为: StatusCollection, 参考该API的定义： 
                // HomeTimeline(ServiceArgument argment, Action<Callback<StatusCollection>> callback);
                statusService.HomeTimeline(new ServiceArgument() { PageFlag = (int)flag, Reqnum = 10, PageTime = pagetime }, (callBack) =>
                {
                    // 因为回调回来之后会是后台线程，需要切换到UI线程去处理ObservableCollection
                    // 相关的操作
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        // 清空Timeline，并把回调回来的数据添加到Timeline里去.
                        //Timeline.Clear();
                        //callBack.Data.ForEach(a =>
                        //{
                        //    //a.Nick = a.Nick + "：" + a.Text;
                        //    DateTime date = new DateTime(1970, 01, 01, 00, 00, 00);
                        //    date = date.AddSeconds(a.Timestamp);
                        //    a.From = date.Month + "月" + date.Day + "日 " + date.Hour + ":" + date.Minute + "  " + a.From;
                        //    if (null == a.Head || a.Head.Equals(""))
                        //    {
                        //        a.Head = "Resources/head_50.jpg";
                        //    }
                        //    switch (a.Type)
                        //    {
                        //        case 2:
                        //            //a.Nick = a.Nick + " 转播：";
                        //            a.Nexttime = " 转播：";
                        //            break;
                        //        case 3:
                        //            //a.Nick = a.Nick + " 私信：";
                        //            a.Nexttime = " 私信：";
                        //            break;
                        //        case 6:
                        //            //a.Nick = a.Nick + " 提及：";
                        //            a.Nexttime = " 提及：";
                        //            break;
                        //        default:
                        //            //a.Nick = a.Nick + "：";
                        //            a.Nexttime = "：";
                        //            break;
                        //    }
                        //    Timeline.Add(a);
                        //});

                        // 切换并回调到UI线程.
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action(callBack);
                            }
                        });
                    });
                });
                //});
            }).Start();
        }

        /// <summary>
        /// 获取或设置当前登陆用户
        /// </summary>
        public User User
        {
            get { return user; }
            set
            {
                if (user != value)
                {
                    user = value;
                    NotifyPropertyChanged("User");
                }
            }
        }

        /// <summary>
        /// 获取或设置首页时间线
        /// </summary>
        public ICollection<Status> Timeline
        {
            get;
            set;
        }
        //public ObservableCollection<Status> Timeline
        //{
        //    get;
        //    set;
        //}
    }
}
